<template>
  <div>
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
<!--          搜索框-->
        <el-form-item prop="storeNo">
          <el-input size="small" v-model="searchForm.storeNo" placeholder="店号" clearable></el-input>
        </el-form-item>
        <el-form-item prop="storeName">
          <el-input size="small" v-model="searchForm.storeName" placeholder="店名" clearable></el-input>
        </el-form-item>
		     <el-form-item prop="ip">
           <el-input size="small" v-model="searchForm.ip" placeholder="ip地址" clearable></el-input>
         </el-form-item>
        <el-form-item prop="processName">
          <el-input size="small" v-model="searchForm.processName" placeholder="进程名称" clearable></el-input>
        </el-form-item>
        <el-form-item prop="beginDate">
          <el-date-picker
            v-model="searchForm.beginDate"
            type="daterange"
            size="small"
            align="right"
            value-format="yyyy-MM-dd"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
         <el-form-item>
          <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
         </el-form-item>
         <el-form-item prop="loginName">
            <el-upload
              class="upload-demo"
              :action="`${this.$http.BASE_URL}/test/tobacco/tobaccoForm/import`"
              :on-success="uploadSuccess"
               :show-file-list="true">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
        </el-form-item>
      </el-form>
      <el-row>
<!--        <el-button v-if="hasPermission('test:tobacco:tobaccoForm:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
        <el-button v-if="hasPermission('test:tobacco:tobaccoForm:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('test:one:leaveForm:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>-->
        <el-button-group class="pull-right">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
<!--            <el-button v-if="hasPermission('test:one:leaveForm:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
            <el-button v-if="hasPermission('test:one:leaveForm:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>-->
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
      :span-method="objectSpanMethod"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        prop="storeName"
        show-overflow-tooltip
        sortable="custom"
        label="店名">
      </el-table-column>
      <el-table-column
        prop="storeNo"
        show-overflow-tooltip
        sortable="custom"
        label="店号">
      </el-table-column>
      <el-table-column
        prop="ip"
        show-overflow-tooltip
        sortable="custom"
        label="ip地址">
      </el-table-column>
      <el-table-column
        prop="startUpTime"
        show-overflow-tooltip
        sortable="custom"
        label="开机时长(小时)">
      </el-table-column>
	  <el-table-column
        prop="processName"
        show-overflow-tooltip
        label="进程名称">
      </el-table-column>
	  <el-table-column
        prop="processTime"
        show-overflow-tooltip
        sortable="custom"
        label="秒计算时间(小时)">
      </el-table-column>
      <el-table-column
        prop="usePer"
        show-overflow-tooltip
        sortable="custom"
        label="软件使用时长占比">
      </el-table-column>
      <el-table-column
        prop="createTime"
        show-overflow-tooltip
        sortable="custom"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        show-overflow-tooltip
        sortable="custom"
        label="更新时间">
      </el-table-column>
<!--      <el-table-column
        prop="gps"
        show-overflow-tooltip
        sortable="custom"
        label="gps地址">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('test:tobacco:tobaccoForm:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('test:tobacco:tobaccoForm:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('test:tobacco:tobaccoForm:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
<!--    <LeaveFormForm  ref="leaveFormForm" @refreshDataList="refreshList"></LeaveFormForm>-->
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import UserSelect from '@/components/userSelect'
  import CityPicker from '@/components/cityPicker'

  export default {
    data () {
      return {
        searchForm: {
          office: {
            id: ''
          },
          tuser: {
            id: ''
          },
          area: '',
          ip: '',
          beginDate: '',
          storeNo: '',
          storeName: '',
          processName: '',
          endDate: '',
          remarks: ''
        },
        dataList: [],
        spanArr: [],
        position: 0,
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: true,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      SelectTree,
      UserSelect,
      CityPicker
    },
    activated () {
      this.refreshList()
    },

    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/test/processTime/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            beginBeginDate: this.searchForm.beginDate[0],
            endBeginDate: this.searchForm.beginDate[1],
            ...this.lodash.omit(this.searchForm, 'beginDate')
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list || []
            this.rowspan(0, 'storeName')
            this.rowspan(1, 'storeName')
            this.rowspan(3, 'ip')
            this.rowspan(2, 'ip')
            for (let i = 0; i < this.dataList.length; i++) {
              let minutes = parseInt(this.dataList[i].processTime % 3600 / 60)
              let hours = parseInt(this.dataList[i].processTime / 3600);
              this.dataList[i].processTime = hours + "小时" + minutes + "分钟"
            }
            this.total = data.page.count
            this.loading = false
          }
        })
      },
    rowspan(idx, prop) {
      this.spanArr[idx] = [] // 存储要合并的数组
      const that = this
      this.position = 0 // 存储要合并的位置
      this.dataList.forEach((item, index) => {
        if (index === 0) {
          that.spanArr[idx].push(1)
          that.position = 0
        } else {
          if (that.dataList[index][prop] === that.dataList[index - 1][prop]) {
            that.spanArr[idx][that.position] += 1 // 有相同项
            that.spanArr[idx].push(0) // 名称相同后往数组里面加一项0
          } else {
            that.spanArr[idx].push(1) // 同列的前后两行单元格不相同
            that.position = index
          }
        }
      })
    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      for (let i = 0; i < this.spanArr.length; i++) {
        if (columnIndex === i) {
          const _row = this.spanArr[i][rowIndex]
          const _col = _row > 0 ? 1 : 0
          return {
            rowspan: _row,
            colspan: _col
          }
        }
      }
    },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.leaveFormForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.leaveFormForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.leaveFormForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/test/tobacco/tobaccoForm/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/test/tobacco/tobaccoForm/import/template')
      },
      exportExcel () {
        this.$utils.download('/test/tobacco/tobaccoForm/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
